package com.newppjoke.ktnewppjoke.utils;

import android.content.ComponentName;

import com.newppjoke.ktnewppjoke.FixFragmentNavigator;
import com.newppjoke.ktnewppjoke.bean.Destination;

import java.util.HashMap;

import androidx.fragment.app.FragmentActivity;
import androidx.navigation.ActivityNavigator;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;

/**
 * Project Name: ktnewppjoke
 * File Name:    NavGraphBuilder.java
 * ClassName:    NavGraphBuilder
 *
 * Description: TODO.
 *
 * @author My start
 * @date 2022年10月10日 7:52 下午
 *
 * Copyright (c) 2022年,  new Network CO.ltd. All Rights Reserved.
 */
public class NavGraphBuilder
{


    public static void build(NavController controller, FragmentActivity  activity,int containerId) {
        NavigatorProvider provider = controller.getNavigatorProvider();

//        FragmentNavigator fragmentNavigator = provider.getNavigator(FragmentNavigator.class);
        //fragment的导航此处使用我们定制的FixFragmentNavigator，底部Tab切换时 使用hide()/show(),而不是replace()
        FixFragmentNavigator fragmentNavigator = new FixFragmentNavigator(activity, activity.getSupportFragmentManager(), containerId);
        provider.addNavigator(fragmentNavigator);

        ActivityNavigator activityNavigator = provider.getNavigator(ActivityNavigator.class);
        NavGraph navGraph = new NavGraph(new NavGraphNavigator(provider));

        HashMap<String, Destination> destConfig = AppConfig.getDestConfig();
        for (Destination value : destConfig.values())
        {
            if (value.isFragment)
            {
                FragmentNavigator.Destination destination = fragmentNavigator.createDestination();
                destination.setClassName(value.className);
                destination.setId(value.id);
                destination.addDeepLink(value.pageUrl);
                navGraph.addDestination(destination);
            }
            else
            {
                ActivityNavigator.Destination destination = activityNavigator.createDestination();
                destination.setId(value.id);
                destination.addDeepLink(value.pageUrl);
                destination.setComponentName(new ComponentName(AppGlobals.getApplication()
                                                                         .getPackageName(), value.className));
                navGraph.addDestination(destination);
            }
            if (value.asStarter){
                navGraph.setStartDestination(value.id);

            }

        }

        controller.setGraph(navGraph);
    }




    public static void build(NavController controller) {
        NavigatorProvider provider = controller.getNavigatorProvider();

        FragmentNavigator fragmentNavigator = provider.getNavigator(FragmentNavigator.class);
        ActivityNavigator activityNavigator = provider.getNavigator(ActivityNavigator.class);

        NavGraph navGraph = new NavGraph(new NavGraphNavigator(provider));

        HashMap<String, Destination> destConfig = AppConfig.getDestConfig();
        for (Destination value : destConfig.values())
        {
            if (value.isFragment)
            {
                FragmentNavigator.Destination destination = fragmentNavigator.createDestination();
                destination.setClassName(value.className);
                destination.setId(value.id);
                destination.addDeepLink(value.pageUrl);
                navGraph.addDestination(destination);
            }
            else
            {
                ActivityNavigator.Destination destination = activityNavigator.createDestination();
                destination.setId(value.id);
                destination.addDeepLink(value.pageUrl);
                destination.setComponentName(new ComponentName(AppGlobals.getApplication()
                                                                         .getPackageName(), value.className));
                navGraph.addDestination(destination);
            }
            if (value.asStarter){
                navGraph.setStartDestination(value.id);

            }

        }

        controller.setGraph(navGraph);
    }


}
